<template>
  <div class="replace-box">
    <el-dialog width="600px" top="5%" title="新增上游充值记录" v-model="dialogFormVisible" :draggable="true" @close="dialogClose">
      <div>
        <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item prop="channelCode" label="上游平台">
                <el-select style="width:90%" :disabled="disab" filterable v-model="dialogForm.channelCode" @change="shopChange"
                  placeholder="请选择上游平台">
                  <el-option v-for="item in shopOptions" :key="item.channelCode" :label="item.channelName"
                    :value="item.channelCode" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="receiveAccount" label="收款账户">
                <el-input style="width:90%;" v-model="dialogForm.receiveAccount" placeholder="请输入收款账户" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="receiveAccountName" label="账户名称">
                <el-input style="width:90%;" v-model="dialogForm.receiveAccountName" placeholder="请输入账户名称" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider border-style="dashed" />
          <el-row>
            <el-col :span="12">
              <el-form-item prop="payFee" label="打款金额">
                <el-input style="width:90%;" v-model="dialogForm.payFee" type="number" min="0" placeholder="请输入打款金额">
                  <template #append>元</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="payTime" label="打款时间">
                <el-date-picker style="width:90% !important;" v-model="dialogForm.payTime" type="datetime" value-format="x"
                  placeholder="请选择打款时间" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="payAccountType" label="打款方式">
                <el-input style="width:90%;" v-model="dialogForm.payAccountType" placeholder="请输入打款方式" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="payAccount" label="打款账户">
                <el-input style="width:90%;" v-model="dialogForm.payAccount" placeholder="请输入打款账户" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="payTradeNo" label="交易流水号">
                <el-input style="width:90%;" v-model="dialogForm.payTradeNo" placeholder="请输入交易流水号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="remarks" label="备注">
                <el-input style="width:90%;" v-model="dialogForm.remarks" placeholder="请输入备注" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider border-style="dashed" />
          <el-row>
            <el-col :span="12">
              <el-form-item prop="password" label="登录密码">
                <el-input style="width:90%" v-model="dialogForm.password" type="password" show-password placeholder="请输入登录密码" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
import hexMD5 from "@/utils/md5.min.js"
import { numFormat, successDeal, warningDeal, accDiv } from '@/utils/utils'
import api from '../api'
export default {
  setup(props, context) {
    const dialogFormRef = ref(null)

    const state = reactive({
      disab: false,
      dialogFormVisible: false,
      dialogForm: {},
      dialogFormRules: {
        channelCode: [
          { required: true, message: '请选择上游平台', trigger: 'blur' },
        ],
        receiveAccount: [
          { required: true, message: '请输入收款账户', trigger: 'blur' },
        ],
        receiveAccountName: [
          { required: true, message: '请输入账户名称', trigger: 'blur' },
        ],
        payFee: [
          { required: true, message: '请输入打款金额', trigger: 'blur' },
          {
            pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: '请输入大于等于0的数字', trigger: 'blur'
          },
        ],
        payTime: [
          { required: true, message: '请选择打款时间', trigger: 'blur' },
        ],
        payAccountType: [
          { required: true, message: '请输入打款方式', trigger: 'blur' },
        ],
        payAccount: [
          { required: true, message: '请输入打款账户', trigger: 'blur' },
        ],
        payTradeNo: [
          { required: true, message: '请输入交易流水号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
        ],
      },
      shopOptions: [],
    })

    function dialogShow(type, data) {
      getShopOptions()
      state.dialogFormVisible = true
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          //校验通过
          const obj = {
            channelCode: state.dialogForm.channelCode || null,
            receiveAccount: state.dialogForm.receiveAccount || null,
            receiveAccountName: state.dialogForm.receiveAccountName || null,
            payFee: state.dialogForm.payFee || null,
            payTime: state.dialogForm.payTime || null,
            payAccountType: state.dialogForm.payAccountType || null,
            payAccount: state.dialogForm.payAccount || null,
            payTradeNo: state.dialogForm.payTradeNo || null,
            password: hexMD5(state.dialogForm.password) || null,
          }
          api.channelRecharge(obj).then(res => {
            if (res.code == '200') {
              successDeal(res.msg)
              context.emit('dialogClose')
              state.dialogFormVisible = false
            } else {
              warningDeal(res.msg)
            }
          }).catch(err => {
            console.log(err)
          })
        }
      })
    }

    function getShopOptions() {
      const obj = {
        page: 1,
        size: 10000,
      }
      api.channelList(obj).then(res => {
        if (res.code == '200') {
          state.shopOptions = res.data.list
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function shopChange(e) {
      state.dialogForm.leftFee = null
      getShopDetail(e)
    }

    function getShopDetail(shopId) {
      const obj = {
        shopId
      }
      api.shopDetail(obj).then(res => {
        if (res.code == '200') {
          state.dialogForm.leftFee = accDiv(res.data.leftFee, 10000)
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function dialogClose() {
      state.disab = false
      dialogFormRef.value.resetFields()
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      dialogClose,
      submitHandle,
      dialogShow,
      numFormat,
      shopChange,
    }
  }
}
</script>

<style lang="scss" scoped>
:deep(.el-divider--horizontal) {
  margin: 5px 0 !important;
}
:deep(.el-input-group__append) {
  padding: 0 20px !important;
}
</style>